package Sj.sj03;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;

public class Sj02 {
    public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException, TransformerException {
      ShowInfo3();
    }

   public static void ShowInfo1() throws IOException {
       Document doc=null;
       try{
           //加载DOM树
           SAXReader saxReader=new SAXReader();
           doc=saxReader.read(new File("src/main/java/Sj/sj03/Jd01.xml"));
       }catch (Exception e){
           e.printStackTrace();
       }
       //获取XML的根节点
       Element root=doc.getRootElement();
       //添加子节点Brand
       Element brandElm=root.addElement("Brand");
       brandElm.addAttribute("name","三星");

       //添加子节点Type
       Element typeElement=brandElm.addElement("Type");
       typeElement.addAttribute("name","NoteX");

       //保存到XML文件中
       OutputFormat format=OutputFormat.createPrettyPrint();
       format.setEncoding("UTF-8");
       XMLWriter writer=new XMLWriter(new FileWriter("src/main/java/Sj/sj03/Jd01.xml"));
       writer.write(doc);
       writer.close();
   }
   public static void ShowInfo2() throws IOException {
       Document doc=null;
       try{
           //加载DOM树
           SAXReader saxReader=new SAXReader();
           doc=saxReader.read(new File("src/main/java/Sj/sj03/Jd01.xml"));
       }catch (Exception e){
           e.printStackTrace();
       }
       //获取XML的根节点
       Element root=doc.getRootElement();
       //遍历所有的Brand标签
       for (Iterator itBrand=root.elementIterator();itBrand.hasNext();){
           Element brand=(Element) itBrand.next();
           Attribute attributeBrand=brand.attribute("name");
           String brandName=attributeBrand.getText();
           if (brandName.equals("三星")){
               attributeBrand.setText("SAMSUNG");
           }
       }

       //保存到XML文件中
       OutputFormat format=OutputFormat.createPrettyPrint();
       format.setEncoding("UTF-8");
       XMLWriter writer=new XMLWriter(new FileWriter("src/main/java/Sj/sj03/Jd01.xml"));
       writer.write(doc);
       writer.close();
   }
    public static void ShowInfo3() throws IOException {
        Document doc=null;
        try{
            //加载DOM树
            SAXReader saxReader=new SAXReader();
            doc=saxReader.read(new File("src/main/java/Sj/sj03/Jd01.xml"));
        }catch (Exception e){
            e.printStackTrace();
        }
        //获取XML的根节点
        Element root=doc.getRootElement();
        //遍历所有的Brand标签
        for (Iterator itBrand=root.elementIterator();itBrand.hasNext();){
            Element brand=(Element) itBrand.next();
            Attribute attributeBrand=brand.attribute("name");
            String brandName=attributeBrand.getText();
            if (brandName.equals("SAMSUNG")){
               root.remove(brand);
            }
        }

        //保存到XML文件中
        OutputFormat format=OutputFormat.createPrettyPrint();
        format.setEncoding("UTF-8");
        XMLWriter writer=new XMLWriter(new FileWriter("src/main/java/Sj/sj03/Jd01.xml"));
        writer.write(doc);
        writer.close();
    }
}
